package com.mall4j.cloud.coupon.mapper;

import com.mall4j.cloud.coupon.model.UserCoupon;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserCouponMapper {
    @Select("select * from user_coupon where user_id=#{userId}")
    List<UserCoupon> getUserCoupon(long userId);
    // 获取订单号是空的，即没使用过的优惠卷
    @Select("select * from user_coupon where user_id=#{userId} and (order_id is null  or order_id='') ")
    List<UserCoupon> getUserCouponNotUse(long userId);
    @Select("select * from user_coupon where user_id=#{userId} and id=#{couponId}")
    UserCoupon findCoupon(@Param("couponId") long couponId,@Param("userId") long userId);
    @Update("update user_coupon set cur_value=#{value},order_id=#{orderId} where id=#{couponId}")
    int updateUserCouponToUse(@Param("couponId") long couponId,@Param("orderId") String orderId,@Param("value")long value);
    @Select("select sum(cur_value) from user_coupon where order_id=#{orderId}")
    long getCouponValue(String orderId);
    int save(UserCoupon userCoupon);
    @Select("select 1 from user_coupon where user_id=#{userId} and coupon_config_id=#{couponId} limit 1")
    Integer checkExistsByConfigIdAndUserId(@Param("userId") long userId, @Param("couponId") int couponId);

}
